package com.primecodegen.common.DataModelProject;

import com.primecodegen.common.MainProject.MainProjectDAO;
import com.primecodegen.common.MainProject.MainProjectTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;

@ManagedBean(name = "dataModelProjectBean")
@RequestScoped
public class DataModelProjectBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(DataModelProjectBean.class.getName());
    private int count;
    private int dataModelProjectID;
    private String excludedClmn;
    private DataModelProjectTable selectedDataModelProject;
    HttpSession session;
    private DataModelProjectDAO dataModelProjectDAO;
    private ArrayList<DataModelProjectTable> dataModelProjectList;
    private DataModelProjectTable dataModelProjectSrchCrit;
    private DataModelProjectTable dataModelProjectUpdate;
    private DataModelProjectTable dataModelProjectTable;
    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public DataModelProjectBean() {
        logger.info(" Constructor DataModelProjectBean()");
        excludedClmn = "6";
        context = getContext();
        session = getSession();
        dataModelProjectDAO = new DataModelProjectDAO();
        dataModelProjectList = new ArrayList<DataModelProjectTable>();
        dataModelProjectTable = new DataModelProjectTable();
        dataModelProjectSrchCrit = (DataModelProjectTable) session.getAttribute("sessionDataModelProjectSrchCrit");
        dataModelProjectUpdate = (DataModelProjectTable) session.getAttribute("dataModelProjectUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (dataModelProjectSrchCrit == null) {
            dataModelProjectSrchCrit = new DataModelProjectTable();
        }

        if (dataModelProjectUpdate == null) {
            dataModelProjectUpdate = new DataModelProjectTable();
        }



        fillDataModelProjectList();
    }

    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public DataModelProjectTable getDataModelProjectUpdate() {
        dataModelProjectUpdate = (DataModelProjectTable) session.getAttribute("dataModelProjectUpdateSess");
        if (dataModelProjectUpdate == null) {
            dataModelProjectUpdate = new DataModelProjectTable();
        }
        return dataModelProjectUpdate;
    }

    public void setDataModelProjectUpdate(DataModelProjectTable dataModelProjectUpdate) {
        session.setAttribute("dataModelProjectUpdateSess", dataModelProjectUpdate);
        this.dataModelProjectUpdate = dataModelProjectUpdate;
    }

    public DataModelProjectBean(DataModelProjectTable dataModelProjectTable) {
        logger.info("Constructor DataModelProjectBean( DataModelProjectTable dataModelProjectTable )");
        this.dataModelProjectTable = dataModelProjectTable;
    }

    public DataModelProjectTable getDataModelProjectTable() {
        return dataModelProjectTable;
    }

    public void setDataModelProjectTable(DataModelProjectTable dataModelProjectTable) {
        this.dataModelProjectTable = dataModelProjectTable;
    }

    public DataModelProjectTable getDataModelProjectSrchCrit() {
        return dataModelProjectSrchCrit;
    }

    public void setDataModelProjectSrchCrit(DataModelProjectTable dataModelProjectSrchCrit) {
        this.dataModelProjectSrchCrit = dataModelProjectSrchCrit;
    }

    public DataModelProjectTable getSelectedDataModelProject() {
        return selectedDataModelProject;

    }

    public void setSelectedDataModelProject(DataModelProjectTable selectedDataModelProject) {
        this.selectedDataModelProject = selectedDataModelProject;
    }

    public ArrayList<DataModelProjectTable> getDataModelProjectList() {
        return dataModelProjectList;
    }
//******************************
    private ArrayList<SelectItem> mainProjectList = new ArrayList<SelectItem>();
    private MainProjectDAO mainProjectDAO = new MainProjectDAO();

    public ArrayList<SelectItem> getMainProjectList() {
        getAllMainProject();
        return mainProjectList;
    }

    public void setMainProjectList(ArrayList<SelectItem> mainProjectList) {
        this.mainProjectList = mainProjectList;
    }

    public void getAllMainProject() {
        mainProjectList = new ArrayList<SelectItem>();
        MainProjectTable groupTable = new MainProjectTable();
        mainProjectList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itMainProject = mainProjectDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itMainProject.hasNext()) {
                groupTable = (MainProjectTable) itMainProject.next();
                mainProjectList.add(new SelectItem(groupTable.getID(), groupTable.getName()));
                groupTable = new MainProjectTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllMainProject() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in DataModelProjectBean");
        try {
            dataModelProjectSrchCrit.setFilter(true);
            this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(dataModelProjectSrchCrit, null);

            session.setAttribute("sessionDataModelProjectSrchCrit", dataModelProjectSrchCrit);
            fillDataModelProjectList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in DataModelProjectBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

    public void clearFilter() {
        try {
            dataModelProjectSrchCrit = null;
            session.setAttribute("sessionDataModelProjectSrchCrit", dataModelProjectSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillDataModelProjectList ()");

        try {

            if (dataModelProjectSrchCrit != null && colToOrderBy != null) {
                this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(dataModelProjectSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dataModelProjectSrchCrit != null) {
                this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(dataModelProjectSrchCrit, null);
            } else {
                this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDataModelProjectList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("dataModelProjectIDSess").toString());
            dataModelProjectUpdate.setID(parID);
            dataModelProjectDAO.updateByID(dataModelProjectUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = dataModelProjectDAO.insert(dataModelProjectTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "DataModelProject Added"));
                fillDataModelProjectList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in dataModelProjectDAO.insert( dataModelProjectTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("dataModelProjectIDSess").toString());



            dataModelProjectDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "DataModelProject Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("dataModelProjectID"));
            dataModelProjectUpdate = (DataModelProjectTable) dataModelProjectDAO.getByID(parID);
            session.setAttribute("dataModelProjectUpdateSess", dataModelProjectUpdate);
            session.setAttribute("dataModelProjectIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(DataModelProjectBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillDataModelProjectList() {
        logger.log(Level.INFO, "Starting: public void fillDataModelProjectList () in DataModelProjectBean");
        try {

            if ((dataModelProjectSrchCrit != null && dataModelProjectSrchCrit.isFilter()) && colToOrderBy != null) {
                this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(dataModelProjectSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dataModelProjectSrchCrit != null && dataModelProjectSrchCrit.isFilter()) {
                this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(dataModelProjectSrchCrit, null);
            } else {
                this.dataModelProjectList = dataModelProjectDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillDataModelProjectList () in DataModelProjectBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDataModelProjectList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }
}
